/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.application.event;

import com.jb.faces.component.NamingContainer;
import com.jb.util.ClassUtil;
import com.jb.web.application.Json;
import com.jb.web.application.Wizard;
import com.jb.web.bean.BaseComponent;
import com.jb.web.bean.BeanFactory;
import com.jb.web.bean.NonDeletable;
import com.jb.web.bean.Page;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.bean.event.UIEvent;
import com.jb.web.context.WebContext;
import com.jb.web.session.Session;


public class ComponentEH {
    
    public void onAddComponent(WebContext ctx, UIEvent e) {

        try {
            Class wizardclass = (Class) BaseComponent.getWizard(ClassUtil.getClass(e.get("itemType"))); 
            //when add a component to the UI, we check if there is a wizard registered against
            //the type of component to be created, if yes, instead of creating the component
            //we will invoke the wizard.
            if(wizardclass!=null)
            {
                 //
                //this will send a message to the client. On receive the message
                //the client will request for the wizard page
                //to display to the user.
                Wizard wizard =(Wizard) wizardclass.newInstance();
                wizard.setPage((Page)ctx.getRequestTree());
                wizard.setParent(e.get("id"));
                Json json = new Json("pageName", wizard.getPageName()).append("panelName", wizard.getPanelName());
                Session session = ctx.getSession();
                session.addAttribute(Session.WIZARD, wizard);
                ctx.writeAJaxResponse(json.toString());
            }else
                BeanFactory.addNewComponent(ctx, e.get("id"), e.get("itemType"));
        }
        catch (Exception _e) {
            ctx.addErrorMessage(this, "Exception at onAddComponent " + e.toString());
            _e.printStackTrace();
        }
    }

    public void onMoveItemTo(WebContext ctx, UIEvent e) {
        String toId = e.get("addTo");

        if (toId == null) {
            ctx.addErrorMessage("moveItem", "addTo is a required parameter");
            return;
        }

        UIComponentImpl comp = (UIComponentImpl)e.getSource();

        if (comp instanceof NonDeletable) {
            return;
        }

        if (comp.isFacet()) {
            return;
        }

        UIComponentImpl parent = 
            (UIComponentImpl)comp.getPage().getDescendantByClientId(toId);

        if (!(parent instanceof NamingContainer) || !parent.isDesignable()) {
            ctx.addErrorMessage("moveItem", 
                                 "Parent must be a valid component to copy to");

            return;
        }
        parent.addChild(comp);
    }
}
